/* ******************************************************************************
 * Copyright Notice:
 * Copyright 2023-2025, Huawei Technologies Co., Ltd. ALL Rights Reserved.
 *
 * Warning: This computer software sourcecode is protected by copyright law
 * and international treaties. Unauthorized reproduction or distribution
 * of this sourcecode, or any portion of it, may result in severe civil and
 * criminal penalties, and will be prosecuted to the maximum extent
 * possible under the law.
 * *******************************************************************************
 */
package com.android.server.am;
 
import com.android.config.EnableRootManager;
 
import android.app.IActivityManager;
import android.graphics.Rect;
import android.os.RemoteException;
 
import java.io.PrintWriter;
 
public class HwActivityManagerShellCommand {
 
    private ActivityManagerShellCommand mCommand;
    private final IActivityManager mInterface;
    private static final int ERR_CODE_UNKNOWN_COMMAND = -1;
    private static final int ERR_CODE_UNKNOWN_OPTION = -2;
 
    private Rect mLaunchBounds;
 
    protected HwActivityManagerShellCommand(ActivityManagerShellCommand command, ActivityManagerService service) {
        mCommand = command;
        mInterface = service;
    }

    public int onCommand(String cmd) {
        if (cmd == null || !EnableRootManager.enableRootManager()) {
            return ERR_CODE_UNKNOWN_COMMAND;
        }
        PrintWriter pw = mCommand.getOutPrintWriter();
        try {
            switch (cmd) {
                case "add-root-permission":
                    return runAddRootPermission();
                case "remove-root-permission":
                    return runRemoveRootPermission();
                case "clear-root-permission":
                    return runClearRootPermission();
                case "show-root-permission":
                    return runShowRootPermission(pw);
                case "root-permission":
                    return runRootPermission(pw);
                default:
                    return ERR_CODE_UNKNOWN_COMMAND;
            }
        } catch (RemoteException e) {
            pw.println("Remote exception: " + e);
        }
        return ERR_CODE_UNKNOWN_COMMAND;
    }
 
    private int runAddRootPermission() throws RemoteException {
        String packageName = mCommand.getNextArgRequired();
        mInterface.addPackageNameToSuWhiteList(packageName);
        return 0;
    }
 
    private int runRemoveRootPermission() throws RemoteException {
        String packageName = mCommand.getNextArgRequired();
        mInterface.removePackageNameFromSuWhiteList(packageName);
        return 0;
    }
 
    private int runClearRootPermission() throws RemoteException {
        mInterface.clearSuWhiteList();
        return 0;
    }
 
    private int runShowRootPermission(PrintWriter pw) throws RemoteException {
        String result = mInterface.getAllPackageNamesOfSuWhiteList();
        pw.println(result);
        return 0;
    }
 
    private int runRootPermission(PrintWriter pw) throws RemoteException {
        String opt = mCommand.getNextOption();
        if (opt == null) {
            mCommand.getErrPrintWriter().println("Error: need 1 option");
            return ERR_CODE_UNKNOWN_OPTION;
        }
 
        String packageName = null;
        if (opt.equals("-a") || opt.equals("-d")) {
            packageName = mCommand.getNextArgRequired();
            if (packageName == null || packageName.length() == 0) {
                mCommand.getErrPrintWriter().println("Error: need 1 argment");
                return ERR_CODE_UNKNOWN_OPTION;
            }
        }
 
        switch (opt) {
            case "-a":
                mInterface.addPackageNameToSuWhiteList(packageName);
                break;
            case "-d":
                mInterface.removePackageNameFromSuWhiteList(packageName);
                break;
            case "-c":
                mInterface.clearSuWhiteList();
                break;
            case "-s":
                String result = mInterface.getAllPackageNamesOfSuWhiteList();
                pw.println(result);
                break;
            default:
                mCommand.getErrPrintWriter().println("Error: Unknown option: " + opt);
                return ERR_CODE_UNKNOWN_OPTION;
        }
        return 0;
    }
 
    protected static void onHelp(PrintWriter pw) {
        if (!EnableRootManager.enableRootManager()) {
            return;
        }
        pw.println("  add-root-permission <PACKAGE>");
        pw.println("    Allow the given application package to have the root access.");
        pw.println("  remove-root-permission <PACKAGE>");
        pw.println("    Prevent the given application package from having the root access.");
        pw.println("  clear-root-permission");
        pw.println("    Remove all the user added application packages which have the root access.");
        pw.println("  show-root-permission");
        pw.println("    Show all the application packages which have the root access.");
        pw.println("  root-permission [-a <PackageName> | -d <PackageName> | -c | -s] ");
        pw.println("      Application root access management.  Options are:");
        pw.println("      -a: Allow the given application package to have the root access.");
        pw.println("      -d: Prevent the given application package from having the root access.");
        pw.println("      -c: Remove all the user added application packages which have the root access.");
        pw.println("      -s: Show all the application packages which have the root access.");
        pw.println();
    }
 
}